﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public partial class FrmSystemOperate : System.Windows.Forms.Form
	{
		public FrmSystemOperate()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
		}

		public FrmSystemOperate(Autodesk.Revit.DB.Document document, string sysName)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			//base.Icon = Resource.YArchitechIcon;
			this.doc = document;
			this.commandSysName = sysName;
			this.projectName = this.doc.Title;
		}

		private void FrmSystemOperate_Load(object sender, EventArgs e)
		{
			this.DrawButtonAndLabel();
		}

		private void DrawButtonAndLabel()
		{
			this.DrawButtonAndLabel(this.commandSysName, this.GetMepSystemsBySysName(this.commandSysName));
			base.Width = this.groupBox1.Width + 20;
			base.Height = this.groupBox1.Height + 35;
		}

		public List<MEPSystem> GetMepSystemsBySysName(string sortSystemName)
		{
			List<MEPSystem> list = new List<MEPSystem>();
			if (sortSystemName == "暖通")
			{
				List<MechanicalSystem> allDuctSystem = this.GetAllDuctSystem();
				if (allDuctSystem != null)
				{
					list.AddRange(allDuctSystem);
				}
				List<PipingSystem> list2 = this.SortPipingSystem(this.GetAllPipeSystem(), "HVAC");
				if (list2 != null)
				{
					list.AddRange(list2);
				}
				if (list.Count > 0)
				{
					return list;
				}
				return null;
			}
			else if (sortSystemName == "消防排水")
			{
				List<PipingSystem> list3 = this.SortPipingSystem(this.GetAllPipeSystem(), "Water");
				if (list3 != null)
				{
					list.AddRange(list3);
				}
				List<PipingSystem> list4 = this.SortPipingSystem(this.GetAllPipeSystem(), "Fire");
				if (list4 != null)
				{
					list.AddRange(list4);
				}
				if (list.Count > 0)
				{
					return list;
				}
				return null;
			}
			else
			{
				if (!(sortSystemName == "电气"))
				{
					return null;
				}
				List<ElectricalSystem> electricalSystem = this.GetElectricalSystem();
				if (electricalSystem != null)
				{
					list.AddRange(electricalSystem);
				}
				if (list.Count > 0)
				{
					return list;
				}
				return null;
			}
		}

		public int DrawButtonAndLabel(string sortSystemName, List<MEPSystem> systemList)
		{
			int num = 15;
			int width = 80;
			if (sortSystemName == "暖通")
			{
				this.groupBox1.Text = "暖通系统列表";
				if (systemList != null)
				{
					foreach (MEPSystem mepSystem in systemList)
					{
						this.DrawControls(mepSystem, this.groupBox1, ref num, ref width);
					}
				}
				this.groupBox1.Width = width;
				this.groupBox1.Height = num + 8;
			}
			if (sortSystemName == "消防排水")
			{
				this.groupBox1.Text = "水系统列表";
				if (systemList != null)
				{
					foreach (MEPSystem mepSystem2 in systemList)
					{
						this.DrawControls(mepSystem2, this.groupBox1, ref num, ref width);
					}
				}
				this.groupBox1.Width = width;
				this.groupBox1.Height = num + 8;
			}
			if (sortSystemName == "电气")
			{
				this.groupBox1.Text = "电气回路列表";
				if (systemList != null)
				{
					foreach (MEPSystem mepSystem3 in systemList)
					{
						this.DrawControls(mepSystem3, this.groupBox1, ref num, ref width);
					}
				}
				this.groupBox1.Width = width;
				this.groupBox1.Height = num + 8;
			}
			return num;
		}

		private List<ElectricalSystem> GetElectricalSystem()
		{
			List<ElectricalSystem> list = new List<ElectricalSystem>();
			FilteredElementIterator elementIterator = new HYSystemElementFilter(this.doc).GetElementsByBICategory(BuiltInCategory.OST_ElectricalCircuit).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				if (element is ElectricalSystem)
				{
					list.Add(element as ElectricalSystem);
				}
			}
			if (list.Count > 0)
			{
				return list;
			}
			return null;
		}

		private List<MechanicalSystem> GetAllDuctSystem()
		{
			List<MechanicalSystem> list = new List<MechanicalSystem>();
			FilteredElementIterator elementIterator = new HYSystemElementFilter(this.doc).GetElementsByBICategory(BuiltInCategory.OST_DuctSystem).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				if (element is MechanicalSystem)
				{
					list.Add(element as MechanicalSystem);
				}
			}
			if (list.Count > 0)
			{
				return list;
			}
			return null;
		}

		private List<PipingSystem> GetAllPipeSystem()
		{
			List<PipingSystem> list = new List<PipingSystem>();
			FilteredElementIterator elementIterator = new HYSystemElementFilter(this.doc).GetElementsByBICategory(BuiltInCategory.OST_PipingSystem).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				if (element is PipingSystem)
				{
					list.Add(element as PipingSystem);
				}
			}
			if (list.Count > 0)
			{
				return list;
			}
			return null;
		}

		private List<PipingSystem> SortPipingSystem(List<PipingSystem> pipesSystem, string typeSys)
		{
			List<PipingSystem> list = new List<PipingSystem>();
			List<PipingSystem> list2 = new List<PipingSystem>();
			List<PipingSystem> list3 = new List<PipingSystem>();
			if (pipesSystem == null || pipesSystem.Count < 1)
			{
				return null;
			}
			foreach (PipingSystem pipingSystem in pipesSystem)
			{
				PipeSystemType systemType = pipingSystem.SystemType;
				if (systemType == 29 || systemType == 7 || systemType == 8 || systemType == 22 || systemType == 17)
				{
					list.Add(pipingSystem);
				}
				if (systemType == 24 || systemType == 26 || systemType == 25 || systemType == 23 || systemType == 28)
				{
					list3.Add(pipingSystem);
				}
				if (systemType == 16 || systemType == 20 || systemType == 19)
				{
					list2.Add(pipingSystem);
				}
			}
			if (list.Count <= 0)
			{
				list = null;
			}
			if (list2.Count <= 0)
			{
				list2 = null;
			}
			if (list3.Count <= 0)
			{
				list3 = null;
			}
			if (typeSys == "HVAC")
			{
				return list;
			}
			if (typeSys == "Water")
			{
				return list2;
			}
			if (typeSys == "Fire")
			{
				return list3;
			}
			return null;
		}

		private void DrawControls(MEPSystem mepSystem, GroupBox groupBox, ref int pointValue, ref int grWidth)
		{
			UCDoubleButton ucdoubleButton = new UCDoubleButton();
			System.Drawing.Point location = new System.Drawing.Point(6, pointValue);
			ucdoubleButton.Location = location;
			ucdoubleButton.btnBrush.Tag = mepSystem;
			ucdoubleButton.btnShowHide.Tag = mepSystem;
			ucdoubleButton.btnBrush.AccessibleDescription = mepSystem.Id.ToString();
			ucdoubleButton.btnShowHide.AccessibleDescription = mepSystem.Id.ToString();
			ucdoubleButton.btnBrush.Image = Image.FromFile(this.brushImage);
			if (mepSystem.Elements != null && mepSystem.Elements.Size > 0)
			{
				ElementSetIterator elementSetIterator = mepSystem.Elements.ForwardIterator();
				Element element = null;
				elementSetIterator.Reset();
				if (elementSetIterator.MoveNext())
				{
					element = (elementSetIterator.Current as Element);
				}
				if (element.IsHidden(this.doc.ActiveView))
				{
					ucdoubleButton.btnShowHide.Image = Image.FromFile(this.hideImage);
					ucdoubleButton.btnShowHide.AccessibleName = "false";
				}
				else
				{
					ucdoubleButton.btnShowHide.Image = Image.FromFile(this.showImage);
					ucdoubleButton.btnShowHide.AccessibleName = "true";
				}
			}
			else
			{
				ucdoubleButton.btnShowHide.Image = Image.FromFile(this.showImage);
			}
			ucdoubleButton.btnBrush.Click += this.ButtonBrush;
			ucdoubleButton.btnShowHide.Click += this.ButtonShowHide;
			groupBox.Controls.Add(ucdoubleButton);
			Label label = new Label();
			label.Text = mepSystem.Name;
			label.AccessibleDescription = mepSystem.Id.ToString();
			location = new System.Drawing.Point(61, pointValue + 10);
			label.Location = location;
			groupBox.Controls.Add(label);
			int width = StringTools.GetStringByteLength(mepSystem.Name) * 8;
			label.Width = width;
			label.Click += this.LabelClick;
			pointValue += 28;
			int num = ucdoubleButton.Height + StringTools.GetStringByteLength(mepSystem.Name) * 10;
			if (grWidth < num)
			{
				grWidth = num + 12;
			}
		}

		private void ButtonShowHide(object sender, EventArgs e)
		{
			Button button = sender as Button;
			if (button == null)
			{
				return;
			}
			MEPSystem mepsystem = button.Tag as MEPSystem;
			this.SelectedLabel(button);
			ElementSet elementSet = new ElementSet();
			Transaction transaction = new Transaction(this.doc, "系统图元隐现");
			transaction.Start();
			if (button.AccessibleName == "false")
			{
				if (mepsystem != null)
				{
					this.doc.ActiveView.UnHideYJKElements(mepsystem.Elements);
				}
				elementSet = this.GetElemSetByParamValue(mepsystem.Name, this.GetElementsBySysName(mepsystem.Name));
				if (elementSet != null && elementSet.Size > 0)
				{
					this.doc.ActiveView.UnHideYJKElements(elementSet);
				}
				button.Image = Image.FromFile(this.showImage);
				button.AccessibleName = "true";
			}
			else
			{
				if (mepsystem != null)
				{
					this.doc.ActiveView.HideYJKElements(mepsystem.Elements);
				}
				elementSet = this.GetElemSetByParamValue(mepsystem.Name, this.GetElementsBySysName(mepsystem.Name));
				if (elementSet != null && elementSet.Size > 0)
				{
					this.doc.ActiveView.HideYJKElements(elementSet);
				}
				button.Image = Image.FromFile(this.hideImage);
				button.AccessibleName = "false";
			}
			transaction.Commit();
		}

		private void ButtonBrush(object sender, EventArgs e)
		{
			Button button = sender as Button;
			if (button == null)
			{
				return;
			}
			this.SelectedLabel(button);
			this.specialSysName = (button.Tag as MEPSystem).Name;
			base.DialogResult = DialogResult.Retry;
		}

		private void LabelClick(object sender, EventArgs e)
		{
			Label label = sender as Label;
			if (label == null)
			{
				return;
			}
			foreach (object obj in this.groupBox1.Controls)
			{
				Label label2 = obj as Label;
				if (label2 != null)
				{
					if (label2.Equals(label))
					{
						label2.BackColor = System.Drawing.Color.LightSkyBlue;
					}
					else
					{
						label2.BackColor = this.groupBox1.BackColor;
					}
				}
			}
		}

		private void SelectedLabel(Button button)
		{
			string accessibleDescription = button.AccessibleDescription;
			foreach (object obj in this.groupBox1.Controls)
			{
				Label label = obj as Label;
				if (label != null)
				{
					if (accessibleDescription.Equals(label.AccessibleDescription))
					{
						label.BackColor = System.Drawing.Color.LightSkyBlue;
					}
					else
					{
						label.BackColor = this.groupBox1.BackColor;
					}
				}
			}
		}

		private ElementSet GetElemSetByParamValue(string paramValue, List<string> elemSetId)
		{
			BuiltInParameter parameterId = BuiltInParameter.RBS_SYSTEM_NAME_PARAM;
			HYSystemElementFilter hysystemElementFilter = new HYSystemElementFilter(this.doc);
			ElementSet elementSet = this.doc.Application.Create.NewElementSet();
			FilteredElementIterator elementIterator = hysystemElementFilter.GetFilterByCondition(null, false, null).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				if (element != null)
				{
					if (elemSetId != null && elemSetId.Count > 0)
					{
						using (List<string>.Enumerator enumerator = elemSetId.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								if (enumerator.Current.Equals(element.Id.ToString()))
								{
									elementSet.Insert(element);
								}
							}
						}
					}
					Parameter parameter = element.GetParameter(parameterId);
					if (parameter != null && parameter.AsString().Equals(paramValue))
					{
						elementSet.Insert(element);
					}
				}
			}
			if (elementSet.Size > 0)
			{
				return elementSet;
			}
			return null;
		}

		public void RecordElementToSystem(string sysName, IList<Element> listElement)
		{
			if (listElement == null || listElement.Count < 1)
			{
				return;
			}
			List<string> list = new List<string>();
			foreach (Element element in listElement)
			{
				list.Add(element.Id.ToString());
			}
			new RecordSystemElements(this.projectName).WriteElementsID(sysName, list);
		}

		private List<string> GetElementsBySysName(string sysName)
		{
			List<string> list = new List<string>();
			list = new RecordSystemElements(this.projectName).GetAllRecordInfo(sysName);
			if (list == null || list.Count < 1)
			{
				return null;
			}
			return list;
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			if (keyData == Keys.Escape)
			{
				base.Close();
				return true;
			}
			return base.ProcessCmdKey(ref msg, keyData);
		}

		private Document doc;

		private string commandSysName = "";

		private string showImage = Path.Combine(Product.ResLocation, "systemShow.png");

		private string hideImage = Path.Combine(Product.ResLocation, "systemHide.png");

		private string brushImage = Path.Combine(Product.ResLocation, "systemBrush.png");

		public string specialSysName = "";

		private string projectName = "";
	}
}
